package org.course.weather.service;

import org.course.weather.entity.WeatherBean;
import org.course.weather.mapper.TWeatherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WeatherService {
    @Autowired
    private TWeatherMapper tWeatherMapper;

    /**
     * 获取所有城市的天气
     * @return
     */
    public List<WeatherBean> getAllWeather() {
        return tWeatherMapper.getAllWeather();
    }

    /**
     * 更新具体一个城市天气
     * @param weatherBean
     * @return
     */
    public void UpdateCityWeather(WeatherBean weatherBean) {
        // 第一步，判断数据库中是否存在该城市
        WeatherBean weatherBean1 = tWeatherMapper.getWeatherByCityId(weatherBean.getId());
        if (weatherBean1 != null) {
            // 存在该城市，更新
            tWeatherMapper.updateCityWeather(weatherBean);
        } else {
            // 不存在该城市，添加
            tWeatherMapper.addCityWeather(weatherBean);
        }
    }
}
